<?php
/**
 * 用户信息类.
 * 高文杰 <gaowenjie_xy@126.com>
 */
import("@.ORG.Validation");
class UserAction extends Action
{
    //修改密码.
    public function changePassword()
    {
        //处理修改密码.
        if ($this->isPost())
        {
            if ($this->_post("oldPassword") == $this->_post("newPassword"))
            {
                $this->error("这新旧密码都一样，你这还想不想改啊！");
            }

            $oldPassword = UserModel::checkPassword($this->_post("oldPassword"));

            if ($oldPassword)
            {
                //验证两次密码是否为空.
                if ($this->_post("newPassword") == '' || $this->_post("checkPassword") == '')
                {
                    $this->error("新密码不能为空哦！");
                }

                //验证两次密码是否一致.
                if ( (md5($this->_post("newPassword")) == md5($this->_post("checkPassword"))))
                {
                    //修改密码.
                    $password = UserModel::updatePassword($this->_post("newPassword"));

                    if ($password)
                    {
                        $this->success("恭喜您,密码修改成功咯！");
                    }
                    else
                    {
                        $this->error("修改密码失败，请联系管理员！");
                    }
                }
                else
                {
                    $this->error("两次密码不一致！");
                }
            }
            else
            {
                $this->error("旧密码输入错误，请重新填写！");
            }
        }

        $this->display();
    }

    //用户信息
    public function userInfo()
    {
        //获取用户信息.
        $user = UserModel::getUserInfo();

        //如果用户信息找不到，则退出系统.
        if (empty($user))
        {
            loginAction::logout();
        }

        $this->assign("user", $user);
        $this->display();
    }

    public function update()
    {
        //获取用户信息.
        $oldUser = UserModel::getUserInfo();

        //如果用户信息找不到，则退出系统.
        if (empty($oldUser))
        {
            loginAction::logout();
        }

        //提交信息.
        if ($this->isPost())
        {
            //实例化User对象.
            $user = M("user");
            //需要提交的数据.
            $u_birthday = date2time($this->_post('u_birthday'));
            $data = array(
                'u_id' => session('u_id'),
                'u_name' => $this->_post('u_name'),
                'u_birthday' => $u_birthday,
                'u_telphone' => $this->_post('u_telphone'),
                'u_qq' => $this->_post('u_qq'),
                'u_email' => $this->_post('u_email'),
                'u_mingyan' => $this->_post('u_mingyan'),
                );

            //如果填写了头像，设置头像.
            if (!empty($_FILES['u_image']['name'][0]))
            {
                import("@.ORG.UploadFile");
                // 实例化上传类
                $upload = new UploadFile();
                // 设置附件上传大小
                $upload->maxSize  = 3145728 ;
                // 设置附件上传类型
                $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
                // 设置附件上传目录
                $upload->savePath =  '../public/photos/';
                $upload->thumb = true;
                $upload->thumbPrefix = PREFIX_PHOTO;
                //缩略图的最大宽度，多个使用逗号分隔
                $upload->thumbMaxWidth = "250,80";
                $upload->thumbMaxHeight = "200,80";

                if (!$upload->upload())
                {
                    // 上传错误提示错误信息
                    $this->error($upload->getErrorMsg());
                }
                else
                {
                    // 上传成功 获取上传文件信息
                    $info =  $upload->getUploadFileInfo();
                    $data['u_image'] = $info[0]['savename'];
                }
            }

            //自动验证数据.
            if ($user->create($data)) 
            {
                if ($user->save($data))
                {
                    commonModel::unlink($oldUser['u_image']);
                    $this->success("恭喜你，修改成功了！", "__URL__/userInfo");
                }
                else
                {
                    //ep($user->getLastSql());
                    $this->error("修改失败，请与管理员联系");
                }
            }
            else
            {
                $this->error($this->error);
            }
        }

        $this->assign("user", $oldUser);
        $this->display();
    }
}
?>